import {
    registerDecorator,
    ValidationOptions,
    ValidationArguments,
} from 'class-validator';
import { SqlOrderEnum, SqlWhereConditionEnum } from '../type/app.dto';

export function IsSqlOrder(
    // property: string,
    validationOptions?: ValidationOptions,
) {
    /**
     *
     * @param object : 要验证的对象
     * @param propertyName : object在类中的名字
     */
    return function (object: object, propertyName: string) {
        // log('object', object);
        // log('propertyName', propertyName);
        registerDecorator({
            name: 'IsSqlOrder',
            target: object.constructor,
            propertyName: propertyName,
            // constraints: [property],
            options: validationOptions,
            validator: {
                /**
                 *
                 * @param value: 验证的对象
                 * @param args： value所属的类完整信息
                 * @returns
                 */
                validate(
                    value: { [key: string]: string },
                    args: ValidationArguments,
                ) {
                    for (const key in value) {
                        // console.log('key', key);
                        if (!(value[key] in SqlOrderEnum)) {
                            // console.log('order 参数', value);
                            return false;
                        }
                    }
                    return true;
                },
            },
        });
    };
}
export function IsSqlWhere(validationOptions?: ValidationOptions) {
    /**
     *
     * @param object : 要验证的对象
     * @param propertyName : object在类中的名字
     */
    return function (object: object, propertyName: string) {
        registerDecorator({
            name: 'IsSqlOrder',
            target: object.constructor,
            propertyName: propertyName,
            options: validationOptions,
            validator: {
                /**
                 *
                 * @param value: 验证的对象
                 * @param args： value所属的类完整信息
                 * @returns
                 */
                validate(value: object, args: ValidationArguments) {
                    for (const item of Object.values(value)) {
                        // console.log('item', item);
                        for (const condition of Object.keys(item)) {
                            if (!(condition in SqlWhereConditionEnum)) {
                                return false;
                            }
                        }
                    }
                    return true;
                },
            },
        });
    };
}
